package com.zx.mobile.service.proxy;

import com.zx.mobile.domain.SysUpdateNeedResponse;
import com.zx.mobile.domain.SysUpdateRequest;
import com.zx.mobile.domain.SysUpdateResponse;
import com.zx.platform.utils.StrUtils;
import com.zx.scp.utils.PropertyUtil;
import org.apache.commons.lang3.StringUtils;

public class SysUpdateCommonProxy {

    /**
     * 系统更新合法性校验
     *
     * @return
     */
    public static String checkSysUpdate(SysUpdateRequest sysUpdateRequest) {
        if (StringUtils.isNotBlank(sysUpdateRequest.getPlatformType())) {
            if (!("A".equals(sysUpdateRequest.getPlatformType()) || "iPhone"
                    .equals(sysUpdateRequest.getPlatformType()))) {
                return "平台类型不合法";
            }
        } else {
            return "输入项必填";
        }
        if (StringUtils.isBlank(sysUpdateRequest.getCurrentVersion())) {
            return "输入项必填";
        }

        return null;
    }

    /**
     * 更新版本检查
     *
     * @param sysUpdateRequest
     * @return
     */
    public static SysUpdateResponse getSysUpdateResponse(
            SysUpdateRequest sysUpdateRequest) {
        SysUpdateResponse sysUpdateResponse = new SysUpdateResponse();
        sysUpdateResponse.setReturnCode("0000");
        sysUpdateResponse.setReturnMsg("系统信息获取成功");
        String downloadLink = null;
        String newVersion = null;
        String updateDesc=null;
        if ("A".equals(sysUpdateRequest.getPlatformType())) {
            downloadLink = PropertyUtil.getValue("androidDownloadLink");
            newVersion = PropertyUtil.getValue("androidNewVersion");
            updateDesc=PropertyUtil.getValue("androidUpdateDesc");
        } else {
            downloadLink = PropertyUtil.getValue("iosDownloadLink");
            newVersion = PropertyUtil.getValue("iosNewVersion");
            updateDesc=PropertyUtil.getValue("iosUpdateDesc");
            if (StrUtils.isNotBlank(PropertyUtil.getValue("iosUpdate"))) {
                sysUpdateResponse.setSysRuntimeStatus("0");
                return sysUpdateResponse;
            }

        }
        if (StringUtils.isNotBlank(newVersion)) {
            sysUpdateResponse.setSysRuntimeStatus("0");
            if (newVersion.equals(sysUpdateRequest.getCurrentVersion())) {//如果最新版本 高于本地版本  需要提醒升级
                sysUpdateResponse.setSysRuntimeStatus("0");
            } else {
                SysUpdateNeedResponse sysUpdateNeedResponse = new SysUpdateNeedResponse();
                sysUpdateNeedResponse.setReturnCode("0000");
                sysUpdateNeedResponse.setReturnMsg("系统信息获取成功");
                sysUpdateNeedResponse.setSysRuntimeStatus("1");
                sysUpdateNeedResponse.setRemark(updateDesc);
                sysUpdateNeedResponse.setLink(downloadLink);
                sysUpdateNeedResponse.setSysRuntimeInfo(newVersion);

                return sysUpdateNeedResponse;
            }
        } else {
            sysUpdateResponse.setReturnCode("9999");
            sysUpdateResponse.setReturnMsg("系统内部异常");
        }
        return sysUpdateResponse;
    }

    public static void  main(String []srgs)
    {
        System.out.println("V2.0.1".compareToIgnoreCase("v2.0.1"));
        System.out.println("V2.0.1".compareToIgnoreCase("v2.0.3"));
        System.out.println("V2.0.1".compareToIgnoreCase("v2.0.0"));
    }
}
